
// code for ResourceTree.mxml

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
import flash.errors.IllegalOperationError;

import navigator.Constants;
import navigator.Helpers;
import navigator.State;

import summit.basecamp.wrappers.AbstractWrapper;
import summit.basecamp.wrappers.CommentWrapper;
import summit.basecamp.wrappers.IChildWrapper;
import summit.basecamp.wrappers.ICommentableWrapper;
import summit.basecamp.wrappers.IListableWrapper;
import summit.basecamp.wrappers.IParentWrapper;
import summit.events.BasecampAPIEvent;

import navigator.BasecampAPIEventHandlers;


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

/** XML for the ArtifactTree */
[Bindable]
public var treeXML:XML = 
<projects name="Projects">
	<active name="Active" id="-1"/>
	<on_hold name="On Hold" id="-1"/>
	<archived name="Archived" id="-1"/>
</projects>;

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

/**
 * Called by Navigator at application complete.
 */
public function wireEvents():void
{
	// The first API call is to get a list of projects (made in BNavCode.as)
	// We need to get the OK event to build the initial tree.
	State.projectWrapper.loader.addEventListener(BasecampAPIEvent.OK,
	BasecampAPIEventHandlers.handleProjectWrapperOKEvent);
}

/** Called after a successful read, to add XML to the tree. **/
public function finishRead(list:XML, xml:XML, items:XML = null):void
{
	if(list) 	// a list of items comes down with the 'read' (todo-list)
	{			// need to process those
		list.appendChild(xml);
	 	State.editData.hasOwnProperty("newItem") == false ?
	 		Helpers.processAPIResult(list, false, State.editData.oldIndex) :
	 		Helpers.processAPIResult(list);
	}
	else Helpers.processAPIResult(xml); 	// single item
	
	// if we're dealing with an item that had an original index - we 
	// respect the index and select the item at the previous index
	if(!State.editData.hasOwnProperty("newItem"))
	{
		selectedItem = selectedItem.children()[State.editData.oldIndex];
	}
	else	// if no old index - we're added to the end of the list 
	{		// we select there
		validateNow();
		selectedItem = (selectedItem as XML).children()[
						(selectedItem as XML).children().length() - 1];
	}

	if(items) Helpers.processAPIResult(items); // add updated items 

	State.editData = new Object();

	handleTreeClickEvent(); // simulate click on the tree
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// General UI Event Handlers

/** Called when the user has clicked on the tree. */
public function handleTreeClickEvent():void
{
	// get the node the user clicked on
	var selectedNode:XML = selectedItem as XML;
	
	// sometimes, the user didn't click on any node... sometimes the
	// node clicked on doesn't require any processing (e.g. root elements
	// that we added for organization.
	if(!selectedNode || selectedNode.@id == -1)
	{
		// in this case, we'll dispatch a let the world know it's not
		// a big deal - and we're finished.
		dispatchEvent(new StatusEvent(StatusEvent.STATUS, false, false,
		"click", "nothing"));
		return;
	}
	
	// to do operations on a resource wrapper representing the selected 
	// tree node - we convert the @resource attribute to a class variable
	var abstractWrapper:AbstractWrapper = 
	Helpers.getClass((selectedItem as XML).@resource);
	
	if(null != abstractWrapper)
	{
		abstractWrapper.loader.accountToken = State.accountToken;
		abstractWrapper.loader.addEventListener(BasecampAPIEvent.OK,
		BasecampAPIEventHandlers.handleListOKEvent);
	}
	
	// if the user clicks on a Basecamp resource type that we're 
	// displaying as a top-level element in the tree - we need to check to 
	// see if Basecamp has items of that type.
	if(selectedNode.children().length() == 0 && selectedNode.@id == -2)
	{
		// all resources corresponding to top-level nodes in the tree 
		// need the project id to get required information from the 
		// Basecamp server
		//
		// each top-level node's parent is a project entry that has attributes
		// specific to the project.
		var projectId:String = selectedNode.parent().@id;
		
		// use the IListable interface to get to .list(projectId) method
		var listable:IListableWrapper = (abstractWrapper as IListableWrapper) 
		
		listable.list(projectId);
			
	}
	else if(selectedNode.children().length() == 0)
	{
		// besides the top-level items, the user can click on individual 
		// items or lists. in some cases (e.g. todo-lists), we need to 
		// download additional items. 
		//
		// in other cases, we may need to get comments or other artifacts
		//
		// in still other cases, clicking an individual item doesn't require
		// ny additional download action.
		
		if(abstractWrapper is IParentWrapper) // Parent Resource Case
		{
			var parentWrapper:IParentWrapper = 
				abstractWrapper as IParentWrapper;
			
			var childResource:IChildWrapper = parentWrapper.child;
						
			childResource.list(selectedNode.@id, 
			selectedNode.parent().parent().@projectId);
		}
		else if(abstractWrapper is ICommentableWrapper && // Commentable case 
		Helpers.doesCommentableHaveComments(selectedNode.@id))
		{
			var commentable:ICommentableWrapper = abstractWrapper as ICommentableWrapper;
			
			var commentWrapper:CommentWrapper = new CommentWrapper(null, 
			abstractWrapper.loader);
			
			commentWrapper.list(selectedNode.@id, commentable.commentableType);
		}
	}
	
	// Now dispatch so that property viewer can handle the click
	dispatchEvent(new StatusEvent(StatusEvent.STATUS, false, false,
		"click", "node"));
}

//////////////////////////////////////////////////////// gmarius@tyemill.com //